package com.tkjt.pms.common.utils;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.PropertyUtilsBean;

import java.beans.PropertyDescriptor;

public class EsBeanUtils extends PropertyUtilsBean {

	public static boolean copyBeanNotNull2Bean(Object databean, Object tobean) {
		PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(databean);
		for (int i = 0; i < origDescriptors.length; i++) {
			String name = origDescriptors[i].getName();
//	          String type = origDescriptors[i].getPropertyType().toString();
			if ("class".equals(name)) {
				continue; // No point in trying to set an object's class
			}
			if (PropertyUtils.isReadable(databean, name) && PropertyUtils.isWriteable(tobean, name)) {
				try {
					Object value = PropertyUtils.getSimpleProperty(databean, name);
					if (value != null) {
						getInstance().setSimpleProperty(tobean, name, value);
					}
				} catch (IllegalArgumentException ie) {
					; // Should not happen
					return false;
				} catch (Exception e) {
					; // Should not happen
					return false;
				}

			}
		}
		return true;
	}

}
